#include "qbdditem.h"

QBDDItem::QBDDItem(QStringList keys,QStringList values)
   : m_type(PART_DEFAULT),m_bddFile(DB_DEFAULT_FILE),m_sort(0)
{
	#ifdef	LOG_ACTIVATED
	__TRACE__
	#endif
	if(keys.size() == values.size())
	{
		for(int i=0;i<keys.size();i++)
			m_properties[keys.at(i)] = values.at(i);
	}
}

QBDDItem::QBDDItem(QByteArray in)
   : m_type(PART_DEFAULT),m_bddFile(DB_DEFAULT_FILE),m_sort(0)
{
	#ifdef	LOG_ACTIVATED
	__TRACE__
	#endif
	#ifdef	LEAK_ACTIVATED
	__LEAK__
	#endif
	QDataStream stream(&in,QIODevice::ReadOnly);
	stream >> m_properties;
}

QBDDItem::~QBDDItem()
{
	#ifdef	LOG_ACTIVATED
	__TRACE__
	#endif
	#ifdef	LEAK_ACTIVATED
	__LEAK__
	#endif
}


QByteArray QBDDItem::toBdd()
{
	#ifdef	LOG_ACTIVATED
	__TRACE__
	#endif
	QByteArray out;
	QDataStream stream(&out,QIODevice::WriteOnly);
	stream << m_properties;

	return out;
}



QString QBDDItem::extractFrom(QString text,QString regexp)
{
	#ifdef	LOG_ACTIVATED
	__TRACE__
	#endif
	#ifdef	LEAK_ACTIVATED
	__LEAK__
	#endif
	QRegExp reg(regexp,Qt::CaseInsensitive);

	if(reg.indexIn(text) != -1)
	{
		return reg.cap();
	}
	else
		return PART_UNKNOWN;
}

quint32 QBDDItem::score(QList<QStringList> rules)
{
	#ifdef	LOG_ACTIVATED
	__TRACE__
	#endif
	#ifdef	LEAK_ACTIVATED
	__LEAK__
	#endif
	quint32 score = 0;
	if(rules.size())
	{
		foreach( QStringList condition,rules)
		{
			if(condition.size() == S_RULES_NUMBER)
			{
				QString field = condition.at(S_FIELD);
				QString search = condition.at(S_SEARCH);
				QString compare = condition.at(S_COMPARE);
				QString trophy = condition.at(S_SCORE);

	

				if(!m_properties.contains(field))
					return 0;
				bool good = false;

				if(compare == S_NOT_EQUAL)
					good = m_properties[field] != search;
				else if(compare == S_EQUAL)
					good = m_properties[field] == search;
				else if(compare == S_GREATER_THAN)
					good = m_properties[field] > search;
				else if(compare == S_GREATER_THAN_OR_EQUAL)
					good = m_properties[field] >= search;
				else if(compare == S_LESSER_THAN)
					good = m_properties[field] < search;
				else if(compare == S_LESSER_THAN_OR_EQUAL)
					good = m_properties[field] <= search;
				else if (compare == S_CONTAINS)
					good = m_properties[field].contains(search);

				if(good)
					score += trophy.toInt();
			}
		}
	}
   m_sort = score;
	return score;
}

QString QBDDItem::get(const QString& value) const
{
	 #ifdef   LOG_ACTIVATED
   __TRACE__
   #endif
   #ifdef   LEAK_ACTIVATED
   __LEAK__
   #endif
	if(m_properties.contains(value))
		return m_properties[value];
	return BDD_UNKNOWN;
}

bool QBDDItem::contains(const QString& search)
{
	#ifdef   LOG_ACTIVATED
   __TRACE__
   #endif
   #ifdef   LEAK_ACTIVATED
   __LEAK__
   #endif
   
   QHash<QString,QString>::iterator it;
   for(it = m_properties.begin();it != m_properties.end(); it++)
	{
		if(it.value().contains(search))
			return true;
	}
	
	return false;
}


 bool QBDDItem::operator <(const QBDDItem& other)
 {
	return this->sort() < other.sort();
}